package com.aivin.web;

import com.aivin.entity.TbPic;
import com.aivin.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  restful设计
 */
@Controller//@Service @Component
@RequestMapping("/pic")// url:/模块/资源/{id}/细分 /seckill/list
public class PicController {

    @Autowired
    private PicService picService  ;


    /**
     * 返回给 app 前端的接口。 get方式
     */
    @RequestMapping(value = "/getAllPicByUserId/{userId}/{offset}/{limit}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<TbPic> getAllPicByUserId(@PathVariable int userId ,  @PathVariable int offset ,@PathVariable int limit  ) {
        List<TbPic> picList =null ;

        if(userId==0){
            picList = picService.getAllPicFy( offset  , limit) ;
        }else{
            picList = picService.getAllPicByUserId(userId,offset  , limit) ;
        }

        return picList;
    }


    /***
     * jsp 页面 访问 ，显示查询结果
     *  userId 为0 时就表示 直接查询所有用户的 素有图片信息。 否则就按 userd进行过滤
     * @param model
     * @return
     */
    @RequestMapping(value = "/piclist/{userId}/{offset}/{limit}", method = RequestMethod.GET)
    public String list(@PathVariable int userId ,  @PathVariable int offset ,@PathVariable int limit ,Model model) {
        //获取列表页
        List<TbPic> list = null;
        if(userId==0){
            list = picService.getAllPicFy( offset  , limit) ;
        }else{
            list = picService.getAllPicByUserId(userId,offset  , limit) ;
        }
        model.addAttribute("piclist", list);
        return "piclist";// /WEB-INF/jsp/"list".jsp
    }


    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String list(Model model) {
        //获取列表页
//        List<Seckill> list = seckillService.getSeckillList();
//        model.addAttribute("list", list);
        //list.jsp + model = ModelAndView
        return "index";// /WEB-INF/jsp/"list".jsp
    }

}
